// index.js
// 获取应用实例
const app = getApp();
const $api = require('../../api/index');

Page({
    data: {
        navTop: app.globalData.navTop,
        navHeight: app.globalData.navHeight,
        indexBannerDetailVOList: [],
        popupBannerDetail: {},
        indexActivityRuleVOList: [
            {
              "unique_id": "291743129594937",
              "name": "充电享特惠",
              "description": "充电享特惠",
              "begin_time": "2022-03-29 00:00:00",
              "end_time": "2024-04-25 00:00:00",
              "cover_img": "http://svfustatics.svfucd.com/1690862547745571.png"
            },
            {
              "unique_id": "291743129594937",
              "name": "敬请期待",
              "description": "敬请期待",
              "begin_time": "2022-03-29 00:00:00",
              "end_time": "2024-04-25 00:00:00",
              "cover_img": "http://svfustatics.svfucd.com/1690863002732545.png"
            }
          ],
        indexOrderMessageVO: {
            message: ""
        },
        active: 0,
        select_station_list: [],
        near_station_list: [],
        discount_station_list: [],
        progress: {},
        consultVOList: []
    },
    onLoad(e) {
        if (e.share_code) {
            wx.setStorageSync("share_code", e.share_code)
        }
        this.getData();
    },
    changeActive(e) {
        var {
            active
        } = e.currentTarget.dataset;
        this.setData({
            active
        })
    },
    gotoDetail({
        mark
    }) { //跳转详情页
        wx.navigateTo({
            url: '../chargeDetail/changeDetail?id=' + mark.id
        })
    },
    getLocation: function () {
        var _this = this;
        wx.getLocation({
            type: 'wgs84',
            success(res) {
                wx.setStorageSync("latitude", res.latitude)
                wx.setStorageSync("longitude", res.longitude)
                _this.getData1({
                    latitude: res.latitude,
                    longitude: res.longitude
                })
            },
            fail() {
                wx.getSetting({
                    success: res => {
                        if (typeof (res.authSetting['scope.userLocation']) != 'undefined' && !res.authSetting['scope.userLocation']) {
                            // 用户拒绝了授权
                            wx.showModal({
                                title: '提示',
                                content: '您拒绝了定位权限，将无法使用定位功能',
                                success: res => {
                                    if (res.confirm) {
                                        // 跳转设置页面
                                        wx.openSetting({
                                            success: res => {
                                                if (res.authSetting['scope.userLocation']) {
                                                    wx.getLocation({
                                                        type: 'wgs84',
                                                        success(res) {
                                                            wx.setStorageSync("latitude", res.latitude)
                                                            wx.setStorageSync("longitude", res.longitude)
                                                            _this.getData1({
                                                                latitude: res.latitude,
                                                                longitude: res.longitude
                                                            })
                                                        },
                                                    })
                                                } else {
                                                    // 没有允许定位权限
                                                    wx.showToast({
                                                        title: '您拒绝了定位权限，将无法使用定位功能',
                                                        icon: 'none',
                                                        duration: 3000
                                                    });
                                                }
                                            }
                                        });
                                    }
                                }
                            });
                        }
                    }
                });
            }
        })
    },
    goToTongzhi: function (e) {
        wx.navigateTo({
            url: `/pages/newsFlash/index?id=${e.currentTarget.dataset.id}`,
        })
    },
    getProgress: function () {
        $api.getProgress().then((res) => {
            if (res.result == 1) {
                this.setData({
                    progress: res.data
                })
            }
        })
    },
    getData: function () {
        $api.homePage().then((res) => {
            if (res.result == 1) {
                this.setData({
                    indexBannerDetailVOList: res.data.indexBannerDetailVOList,
                    consultVOList: res.data.consultVOList,
                    popupBannerDetail: res.data.popupBannerDetail,
                    // indexActivityRuleVOList: res.data.indexActivityRuleVOList,
                    "indexOrderMessageVO.message": res.data.indexOrderMessageVO ? res.data.indexOrderMessageVO.message : ""
                });
                this.getProgress();
                this.getLocation();
            }
        })
    },
    getData1: function () {
        $api.homePage1().then((res) => {
            if (res.result == 1) {
                this.setData({
                    select_station_list: res.data.select_station_list,
                    near_station_list: res.data.near_station_list,
                    discount_station_list: res.data.discount_station_list
                });
            }
        })
    },
    goToUrl: function (e) {
        switch (e.currentTarget.dataset.type) {
            case "1":
                wx.navigateTo({
                    url: '/pages/businessTalkmy/index',
                  }).then(res=>{
                      console.log(res)
                  })
                // wx.navigateToMiniProgram({
                //     appId: 'wxf02b6affdf44fe96',
                //     path: 'pages/home/index',
                //     envVersion: 'release', 
                //     success(res) {
                //       console.log("跳转小程序成功！",res);
                //     } 
                // });
                break;
            case "2":
                wx.switchTab({
                    url: '/pages/charge/charge',
                })
                break;
            case "3":
                wx.navigateTo({
                    url: '/pages/businessTalk/index',
                });
                // wx.navigateTo({
                //     url: '/pages/recreation/index',
                // });
                break;
            case "4":
                wx.navigateTo({
                    url: '/pages/businessTalk/index',
                });
                // wx.navigateTo({
                //     url: '/pages/carSteed/index',
                // });
                break;
        }
    },
    goToPage: function (e) {
        wx.navigateTo({
            url: e.currentTarget.dataset.page,
        });
    },
    goToBanner: function (e) {
        wx.navigateTo({
            url: `/pages/webView/index?url=${e.currentTarget.dataset.url}`,
        });
    },
    onClick: function (params) {
        wx.navigateTo({
            url: '/pages/customerService/customerService',
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        if (typeof this.getTabBar === 'function' &&
            this.getTabBar()) {
            this.getTabBar().setData({
                selected: 0
            })
        }
    }
})